<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #tableTitle>
        <a-button type="primary" @click="handlerReadAllMsg">全部标注已读</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <DetailModal @register="register" />
  </div>
</template>
<script lang="ts" name="monitor-mynews" setup>
import { ref, onMounted } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import DetailModal from './DetailModal.vue';
import { updateAllUnreadMessage } from '../messageApi';
import { getMessagePage,updateReadStatusMessage,getMsgSend } from '/@/api/common/api';
import { columns, searchFormSchema } from './mynews.data';
import { useModal } from '/@/components/Modal';
const checkedKeys = ref<Array<string | number>>([]);
const searchInfo = { logType: '1' };
const [register, { openModal: openDetail }] = useModal();
import { useListPage } from '/@/hooks/system/useListPage';
import { useAppStore } from '/@/store/modules/app';
import { useMessageHref } from '/@/views/system/message/components/useSysMessage';

const appStore = useAppStore();

const { goPage } = useMessageHref();

const { prefixCls, tableContext } = useListPage({
  designScope: 'mynews-list',
  tableProps: {
    title: '我的消息',
    api: getMessagePage,
    columns: columns,
    formConfig: {
      schemas: searchFormSchema,
      //我的消息列表不能通过时间范围查询---
      fieldMapToTime: [['sendTime', ['beginDate', 'endDate'], 'YYYY-MM-DD']],
    },
  },
});
const [registerTable, { reload }] = tableContext;
/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '查看',
      onClick: handleDetail.bind(null, record),
    },
  ];
}

/**
 * 查看
 */
function handleDetail(record) {
  // debugger
  let id = record.id;
  updateReadStatusMessage({ id: id }).then((res) => {
    reload();
  });
  const openModalFun = () => {
    openDetail(true, {
      record,
      isUpdate: true,
    });
  }
  goPage(record, openModalFun);

}
// 日志类型
function callback(key) {
  searchInfo.logType = key;
  reload();
}

//全部标记已读
async function handlerReadAllMsg() {
  await updateAllUnreadMessage();
  reload();
}

/**
 * 选择事件
 */
function onSelectChange(selectedRowKeys: (string | number)[]) {
  checkedKeys.value = selectedRowKeys;
}

//update-begin-author:taoyan date:2022-8-23 for: 消息跳转，打开详情表单
onMounted(() => {
  initHrefModal();
});

function initHrefModal() {
  let params = appStore.getMessageHrefParams;
  if (params) {
    // $message.info('有参数......');
    let id = params.id;
    if (id) {
      updateReadStatusMessage({ id: id }).then(() => {
        reload();
      });
    }
    let detailId = params.detailId;
    if (detailId) {
      getMsgSend(detailId).then(data => {
        console.log('getOne', data)
        openDetail(true, {
          record: data,
          isUpdate: true,
        });
        appStore.setMessageHrefParams('')
      })
    }
  }
}
//update-end-author:taoyan date:2022-8-23 for: 消息跳转，打开详情表单


</script>
